//
//  DetailTopicVC.m
//  NeteaseNewsLY
//
//  Created by dllo on 16/1/23.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "DetailTopicVC.h"
#import "LYPrefixHeader.pch"
@interface DetailTopicVC ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *dataArr;
@property (nonatomic, retain)NSMutableArray *headArr;
@property (nonatomic, retain)NSMutableDictionary *headDic;
// 边距
@property (nonatomic, assign)UIEdgeInsets inserts;
@property(nonatomic, retain)NSDictionary *expertDic;/** tableView头部区域相关信息的字典*/
@end

@implementation DetailTopicVC
- (void)dealloc
{
    [_tableView release];
    [_dataArr release];
    [_headArr release];
    [_headDic release];
    [_expertDic release];
    [super dealloc];
}
- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.alpha = 1;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationController.navigationBar.alpha = 0;
    // 标签栏变透明
//    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefaultPrompt];
    //    // 导航条带的首页 让他消失
//    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView release];
    
    // 数据处理
    [self data];
    
    [_tableView registerClass:[DetailTopicCell class] forCellReuseIdentifier:@"cell"];
    
    NSString *url = self.str;
    // 拼接网址
    NSString *str = [NSString stringWithFormat:@"http://c.3g.163.com/newstopic/qa/%@.html", url];
    //    [AFHTTPHandler GETWithURL:str completion:^(id result) {
    
    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    sessionManager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [sessionManager GET:str parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        self.headDic = responseObject[@"data"][@"expert"];
        // view数据
        [self createView];
        
        self.title = _expertDic[@"alias"];//********
        
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@", error);
    }];


    
}
// 创建头部View
- (void)createView
{
    UIView *headerView = [[UIView alloc] init];
//    headerView.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:headerView];
    [headerView release];
    // 图片
    UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH * 375, HEIGHT * 210)];
    [headerView addSubview:img];
    [img sd_setImageWithURL:[NSURL URLWithString:_headDic[@"picurl"]]];
    [img release];
    img.userInteractionEnabled = YES;
    // 图片上文字
    UILabel *text = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH * 40, HEIGHT * 75, WIDTH * 295, HEIGHT * 50)];
    text.text = _headDic[@"alias"];
    text.numberOfLines = 0;
    text.textColor = [UIColor whiteColor];
    [img addSubview:text];
    [text release];
    // 框
    UIImageView *kuang = [[UIImageView alloc] init];
    kuang.image = [UIImage imageNamed:@"white"];
    [headerView addSubview:kuang];
    [kuang release];
    
    // 头像
    UIImageView *headpicurl = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH * 10, HEIGHT * 10, WIDTH * 30, HEIGHT * 30)];
    [headpicurl sd_setImageWithURL:[NSURL URLWithString:_headDic[@"headpicurl"]]];
    headpicurl.layer.cornerRadius = 15;
    headpicurl.clipsToBounds = YES;
    [kuang addSubview:headpicurl];
    [headpicurl release];
    
    // 名字 职业
    UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH * 50, HEIGHT * 10, WIDTH * 150, HEIGHT * 20)];
    name.text = [[_headDic[@"name"] stringByAppendingString:@"|"]stringByAppendingString:_headDic[@"title"]];
    // 文字字体/颜色
    name.textColor = [UIColor grayColor];
    name.font = [UIFont systemFontOfSize:12];
    [kuang addSubview:name];
    [name release];
    // 详细介绍
    UILabel *content = [[UILabel alloc] init];
    content.text = _headDic[@"description"];
    // 自定义高度
    CGFloat hH = [DetailTopicCell heightWithQuestion:content.text];
    content.frame = CGRectMake(WIDTH * 50, HEIGHT * 25, WIDTH * 295, HEIGHT * hH);
    content.textColor = [UIColor grayColor];
    content.font = [UIFont systemFontOfSize:14];
    content.numberOfLines = 0;
    [kuang addSubview:content];
    [content release];
    // 提问数量/回复数量
    UILabel *wen = [[UILabel alloc] init];
    wen.text = [NSString stringWithFormat:@"%@提问 · %@回复", _headDic[@"questionCount"], _headDic[@"answerCount"]];
    wen.font = [UIFont systemFontOfSize:12];
    wen.textColor = [UIColor grayColor];
    [kuang addSubview:wen];
    [wen release];
    
    wen.frame = CGRectMake(WIDTH * 10, HEIGHT * (40 + hH), WIDTH * 150, HEIGHT * 25);
    kuang.frame = CGRectMake(WIDTH * 10, HEIGHT * 215, WIDTH * 355, HEIGHT * 65 + hH);
    headerView.frame = CGRectMake(0, 0, WIDTH * 375, HEIGHT * (hH + 320));
    self.tableView.tableHeaderView = headerView;

}
- (void)data
{
    // 接收网址
    NSString *url = self.str;
    // 拼接网址
    NSString *str = [NSString stringWithFormat:@"http://c.3g.163.com/newstopic/qa/%@.html", url];

    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    sessionManager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [sessionManager GET:str parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        self.dataArr = responseObject[@"data"][@"hotList"];
        // 头部数据
        self.headDic = responseObject[@"data"][@"expert"];
        // 头部视图
        self.navigationItem.title = _headDic[@"alias"];//********
        
        [self createView];
        [self.tableView reloadData];

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@", error);
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailTopicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    NSDictionary *question = _dataArr[indexPath.row];
    [cell.userHeadPicUrl sd_setImageWithURL:[NSURL URLWithString:question[@"question"][@"userHeadPicUrl"]]];
    cell.userName.text = question[@"question"][@"userName"];
    cell.content.text = question[@"question"][@"content"];
    NSDictionary *answer = _dataArr[indexPath.row];
    [cell.specialistHeadPicUrl sd_setImageWithURL:[NSURL URLWithString:answer[@"answer"][@"specialistHeadPicUrl"]]];
    cell.specialistName.text = answer[@"answer"][@"specialistName"];
    cell.answercontent.text = answer[@"answer"][@"content"];
    cell.supportCount.text = [answer[@"answer"][@"supportCount"] stringValue];
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat queh = [DetailTopicCell heightWithQuestion:self.dataArr[indexPath.row][@"question"][@"content"]];
    CGFloat ansh = [DetailTopicCell heightWithQuestion:_dataArr[indexPath.row][@"answer"][@"content"]];
    
    return HEIGHT * (queh + ansh + 200);
}
//返回按钮点击方法
- (void)buttonClick:(UIButton *)button
{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
